#!/usr/bin/env python3
#
# lscgroup functionality test - multiple '-g' flags
#
# Copyright (c) 2021 Oracle and/or its affiliates.
# Author: Tom Hromatka <tom.hromatka@oracle.com>
#

#
# This library is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License as
# published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, see <http://www.gnu.org/licenses>.
#

from cgroup import Cgroup, CgroupVersion
import consts
import ftests
import os
import sys
import utils

CONTROLLER = 'cpuset'
PARENT_CGNAME = '032lscgroup'
CHILD_CGNAME = 'childlscgroup'
GRANDCHILD_CGNAME = 'grandchildlscgroup'
SIBLING_CGNAME = '032sibling'
SIBLING_CHILD_CGNAME = 'cousinlscgroup'

# lscgroup is inconsistent in its handling of trailing slashes
#
# When invoking lscgroup with no flags, no trailing slashes are present in
# any of the cgroups.
#
# When invoking lscgroup with the -g flag, a trailing slash is present on
# the first cgroup returned (i.e. the cgroup specified in the -g flag)
#
EXPECTED_OUT1 = '''{}:/{}/
{}:/{}/{}
{}:/{}/{}/{}
{}:/{}/
{}:/{}/{}'''.format(CONTROLLER, PARENT_CGNAME,
                    CONTROLLER, PARENT_CGNAME, CHILD_CGNAME,
                    CONTROLLER, PARENT_CGNAME, CHILD_CGNAME, GRANDCHILD_CGNAME,
                    CONTROLLER, SIBLING_CGNAME,
                    CONTROLLER, SIBLING_CGNAME, SIBLING_CHILD_CGNAME)

def prereqs(config):
    result = consts.TEST_PASSED
    cause = None

    return result, cause

def setup(config):
    Cgroup.create(config, CONTROLLER, PARENT_CGNAME)
    Cgroup.create(config, CONTROLLER, os.path.join(PARENT_CGNAME, CHILD_CGNAME))
    Cgroup.create(config, CONTROLLER,
                  os.path.join(PARENT_CGNAME, CHILD_CGNAME, GRANDCHILD_CGNAME))

    Cgroup.create(config, CONTROLLER, SIBLING_CGNAME)
    Cgroup.create(config, CONTROLLER,
                  os.path.join(SIBLING_CGNAME, SIBLING_CHILD_CGNAME))

def test(config):
    result = consts.TEST_PASSED
    cause = None

    out = Cgroup.lscgroup(config, controller=[CONTROLLER, CONTROLLER],
                          path=[PARENT_CGNAME, SIBLING_CGNAME])
    if out != EXPECTED_OUT1:
        result = consts.TEST_FAILED
        cause = "Expected lscgroup output doesn't match received output\n" \
                "Expected:\n{}\n" \
                "Received:\n{}\n".format(utils.indent(EXPECTED_OUT1, 4),
                                         utils.indent(out, 4))
        return result, cause

    ret = Cgroup.lscgroup(config, cghelp=True)
    if not "Usage:" in ret:
        result = consts.TEST_FAILED
        cause = "Failed to print help text"
        return result, cause

    return result, cause

def teardown(config):
    Cgroup.delete(config, CONTROLLER, PARENT_CGNAME, recursive=True)
    Cgroup.delete(config, CONTROLLER, SIBLING_CGNAME, recursive=True)

def main(config):
    [result, cause] = prereqs(config)
    if result != consts.TEST_PASSED:
        return [result, cause]

    setup(config)
    [result, cause] = test(config)
    teardown(config)

    return [result, cause]

if __name__ == '__main__':
    config = ftests.parse_args()
    # this test was invoked directly.  run only it
    config.args.num = int(os.path.basename(__file__).split('-')[0])
    sys.exit(ftests.main(config))
